package com.study.mike.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author king james
 * @create 2021-12-09-{TIME}
 */
@Slf4j
//把更新组件加入到ioc容器中

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 新增时的填充策略
     *
     * @param metaObject
     */

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill....");
        //setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject 对应的属性名、插入的值、metaObject
        this.setFieldValByName("createTime", new Date(), metaObject);
    }

    /**
     * 更新的时候填充策略
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill....");
        this.setFieldValByName("modifyTime", new Date(), metaObject);
    }
}
